package com.handpay.nocardpay.baselibrary.raindrops;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.handpay.nocardpay.baselibrary.logutils.HPLog;

/**
 * Created by sxshi on 2018-4-14.
 * 自定义WebView内核
 */

public class MyWebViewClient extends WebViewClient {
    private Context mContext;
    private String mFailingUrl = null;
    private Handler mHandler;
    private long timeout = 20_000;
    private boolean isShowLoading = false;


    private boolean hasError = false;

    private WebViewTimeOutRunable timeOutRunable;

    public void setShowLoading(boolean showLoading) {
        isShowLoading = showLoading;
    }


    public MyWebViewClient(Context mContext, Handler handler) {
        this.mContext = mContext;
        this.mHandler = handler;
    }

    public long getTimeout() {
        return timeout;
    }

    public void setTimeout(long timeout) {
        this.timeout = timeout;
    }

    public String getmFailingUrl() {
        return mFailingUrl;
    }

    private static final String TAG = "ssx-webclient";

    @Override
    public void onPageStarted(final WebView view, String url, Bitmap favicon) {
        if (isShowLoading) {
            LoadingManager.getInstance((Activity) mContext).showLoading();
        }
        timeOutRunable = new WebViewTimeOutRunable(url);
        mHandler.postDelayed(timeOutRunable, timeout);
    }

    @SuppressWarnings("deprecation")
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (!TextUtils.isEmpty(url)) {
            view.loadUrl(url);
            return true;
        }
        return super.shouldOverrideUrlLoading(view, url);
    }


    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        String url = request.getUrl().toString();
        if (!TextUtils.isEmpty(url)) {
            view.loadUrl(url);
            return true;
        }
        return super.shouldOverrideUrlLoading(view, request);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        hasError = true;
        mFailingUrl = request.getUrl().toString();
        super.onReceivedError(view, request, error);
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        mFailingUrl = failingUrl;
        hasError = true;
        super.onReceivedError(view, errorCode, description, failingUrl);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        HPLog.d(HPLog.SSX_TAG, "url=" + url);
        isShowLoading = false;
        LoadingManager.getInstance((Activity) mContext).dismissLoading();
        mHandler.removeCallbacks(timeOutRunable);
        mHandler.removeMessages(Constants.WEBVIEW_ERROR);
        if (hasError) {
            Message msg = Message.obtain();
            msg.what = Constants.WEBVIEW_ERROR;
            mHandler.sendMessage(msg);
            hasError = false;
        }

        if (url.lastIndexOf("/step1") > 0) {
            Message msg = Message.obtain();
            msg.what = Constants.NOTICE_UPLOAD;
            mHandler.sendMessage(msg);
        }
    }

    /**
     * 销毁对象
     */
    public void destory() {
        LoadingManager.getInstance((Activity) mContext).dismissLoading();
        mHandler.removeCallbacks(timeOutRunable);
        mHandler.removeMessages(Constants.WEBVIEW_ERROR);
        mHandler=null;
    }

    class WebViewTimeOutRunable implements Runnable {
        public WebViewTimeOutRunable(String url) {
            mFailingUrl = url;
        }

        @Override
        public void run() {
            Message msg = Message.obtain();
            msg.what = Constants.WEBVIEW_ERROR;
            mHandler.sendMessage(msg);
        }
    }
}
